<template>
  <div class="gitalk-wrap" >
    <div  id="comments"></div>
  </div>
</template>

<script>
  import 'gitalk/dist/gitalk.css'
  import Gitalk from 'gitalk'
  import {getGitalkIssue} from "@/api/home";

  export default {
    name: "Gitalk",
    data() {
      return {
        comments: -1
      }
    },
    methods: {
      getGitalkIssue() {
        getGitalkIssue().then(({data}) => {
          let bodys = []
          // bodys = data.map((item)=>{
          //   return item.body
          // })
          let res = data.filter((item) => item.body.indexOf(this.$route.path) > -1)[0]
          if(res && res.comments) {
            this.$store.commit("setStoreValue", {
              comments: res.comments
            })
          }else{
            this.$store.commit("setStoreValue", {
              comments: 0
            })
          }

        })
      }
    },
    mounted() {
      this.$nextTick(() => {
        if (this.$route.path) {
          const gitalk = new Gitalk({
            clientID: '8d781747b77d97ee531f',
            clientSecret: '183274be2925ae608e07eefb69e54926703f6f48',
            repo: 'solo-blog',
            owner: 'Arrowfield',
            admin: ['Arrowfield'],
            id: this.$route.path,
            distractionFreeMode: false,
            language: "zh-CN",
          })
          gitalk.render('comments')
        }
      })
      this.getGitalkIssue()
    }
  }
</script>

<style lang="scss" scoped>
  .gitalk-wrap {
    position: relative;
    z-index: 1;
    color: #5f5f5f;
    background-color: #f7f7f7;

    /*#gitalk-container {*/
    /*  max-width: 970px;*/
    /*  margin: 0 auto;*/
    /*  padding: 20px 20px 0 20px;*/
    /*  padding-bottom: 1px;*/
    /*}*/
  }

</style>
